﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace iTextSharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("[START PROGRAM]");

            using (System.IO.FileStream fs = new System.IO.FileStream("First PDF document.pdf", System.IO.FileMode.Create))
            {
                // Create an instance of the document class which represents the PDF document itself.
                //Document document = new Document(PageSize.A4, 25, 25, 30, 30);
                Document document = new Document();

                // Create an instance to the PDF file by creating an instance of the PDF 
                // Writer class using the document and the filestrem in the constructor.
                PdfWriter writer = PdfWriter.GetInstance(document, fs);

                // Add meta information to the document
                //document.AddAuthor("Ong Zhi Xian");
                //document.AddCreator("Sample application using iTextSharp");
                //document.AddKeywords("PDF tutorial demo learn");
                //document.AddSubject("Document subject - Describing the steps creating a PDF document");
                //document.AddTitle("The document title - PDF creation using iTextSharp");

                // Open the document to enable you to write to the document
                document.Open();
                // Add a simple and wellknown phrase to the document in a flow layout manner
                document.Add(new Paragraph("Hello World!"));
                // Close the document
                document.Close();
                // Close the writer instance
                writer.Close();

                // Always close open filehandles explicity
                //fs.Close();
            }

            // Code for writing to memory stream
            //using (MemoryStream ms = new MemoryStream())
            //{
            //    Document document = new Document(PageSize.A4, 25, 25, 30, 30);
            //    PdfWriter writer = PdfWriter.GetInstance(document, ms);
            //    document.Open();
            //    document.Add(new Paragraph("Hello World"));
            //    document.Close();
            //    writer.Close();
            //    Response.ContentType = "pdf/application";
            //    Response.AddHeader("content-disposition",
            //    "attachment;filename=First PDF document.pdf");
            //    Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
            //}

            Console.WriteLine("[END PROGRAM]");
            Console.ReadLine();
        }
    }
}


//ref
//http://www.codeproject.com/Articles/686994/Create-Read-Advance-PDF-Report-using-iTextSharp-in
//http://www.c-sharpcorner.com/UploadFile/f2e803/basic-pdf-creation-using-itextsharp-part-i/